package com.atguigu.paymentdemo.service.impl;

import com.atguigu.paymentdemo.entity.OrderInfo;
import com.atguigu.paymentdemo.entity.ProductInfo;
import com.atguigu.paymentdemo.enums.OrderStatus;
import com.atguigu.paymentdemo.mapper.OrderInfoMapper;
import com.atguigu.paymentdemo.service.OrderInfoService;
import com.atguigu.paymentdemo.service.ProductInfoService;
import com.atguigu.paymentdemo.uitl.SnowflakeIdWorker;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.time.Duration;
import java.time.Instant;
import java.util.List;

@Slf4j
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {
    @Resource
    ProductInfoService productInfoService;
    @Resource
    SnowflakeIdWorker snowflakeIdWorker;

    @Resource
    OrderInfoMapper orderInfoMapper;


    /**
     * 通过请求商品ID生成订单实体
     * 订单实体中没有引入userid，后续开发提供
     * @param productId
     * @return OrderInfo
     */

    public OrderInfo orderInfoGenerate(Long productId,Long userId){
        //通过请求商品ID生成商品
        ProductInfo productInfo = productInfoService.getById(productId);

        //通过订单信息生成订单实体
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setTitle("test");

        orderInfo.setOrderNo(snowflakeIdWorker.nextId());

        orderInfo.setProductId(productId);
        orderInfo.setUserId(userId);
        orderInfo.setTotalFee(productInfo.getPrice());//分
        orderInfo.setOrderStatus(OrderStatus.NOTPAY.getType());

        log.info(orderInfo.toString());
        return orderInfo;

    }

    @Override
    public List<OrderInfo> listDescTimeStamp() {

        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");

        return list(queryWrapper);
    }

    /**
     * 根据订单号更新订单状态
     * @param trade_no
     * @param tradeState
     */

    @Override
    public void ChangeTradeState(String trade_no,String tradeState) {
        log.info("修改订单状态");
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no",trade_no);

        OrderInfo orderInfo = this.getOne(queryWrapper);

        orderInfo.setOrderStatus(tradeState);
        this.update(orderInfo,queryWrapper);
    }

    /**
     * 查询订单状态
     * @param trade_no
     * @return
     */
    public String queryTradeState(String trade_no){

        return queryOrder(trade_no).getOrderStatus();
    }

    /**
     * 根据订单号查询订单
     * @param trade_no
     * @return
     */

    public OrderInfo queryOrder(String trade_no){
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no",trade_no);
        OrderInfo orderInfo = this.getOne(queryWrapper);
        return orderInfo;
    }

    /**根据orderNO获取商品金额
     *
     * @param orderNO
     * @return
     */
    @Override
    public Integer queryOrderTotalFeeByOrderNo(String orderNO) {
        return  queryOrder(orderNO).getTotalFee();
    }

    /**
     * 获取超时订单时间
     * @param minus
     * @return
     */
    public List<OrderInfo> getNoPayOrderOvertime(long minus){
        //5分钟前的时间
        Instant instant = Instant.now().minus(Duration.ofMinutes(minus));
        //log.info("查询"+ instant +"前的未支付订单中...");

        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_status",OrderStatus.NOTPAY.getType());
        queryWrapper.le("create_time",instant);

        List<OrderInfo> res = orderInfoMapper.selectList(queryWrapper);
        //log.info("未支付订单:");

        return res;
    }


}
